#include "Entity.h"

typedef float matrix3x4[3][4];

bool Entity::GetBone(int index, SD_Tools::Vector* vIn)
{
	//if(index>=50||index<0)
	//	return false;
	//if(*boneptr()==0)
	//	return false;
	//BoneVector *bv=&((*boneptr())->Bones[index]);
	//vIn->x=bv->x;
	//vIn->y=bv->y;
	//vIn->z=bv->z;
	//return true;

	typedef PVOID (__thiscall* GetRender_t)(PVOID);
	PVOID pRenderable = VMTWrapper::getfunction<GetRender_t>(this, 5)(this);
	if(!pRenderable)
		return false;

	matrix3x4 matrix[128];
	typedef bool (__thiscall* SetupBones_t)(PVOID, matrix3x4*, int, int, float);
	VMTWrapper::getfunction<SetupBones_t>(pRenderable, 16)(pRenderable, matrix, 128, 0x100, g_pInterfaces.g_pGlobals->curtime);

    vIn->x = matrix[index][0][3];
    vIn->y = matrix[index][1][3];
    vIn->z = matrix[index][2][3];
	return true;
}

void Entity::GetVelocity(SD_Tools::Vector& vel)
{
	typedef void (__thiscall* EstimateAbsVelocity_t)(Entity*, SD_Tools::Vector&);
	EstimateAbsVelocity_t EstimateAbsVelocity = (EstimateAbsVelocity_t)(Offsets::EstimateAbsVelocity);
	EstimateAbsVelocity(this, vel);
}

CBaseCombatWeapon* Entity::GetBaseCombatWeapon()
{
	CBaseHandle hActiveWeapon = *reinterpret_cast<CBaseHandle*>((DWORD)this + Offsets::m_hActiveWeapon); 
	return (CBaseCombatWeapon*)(g_pInterfaces.g_pEntList->GetClientEntityFromHandle(hActiveWeapon)); 
}

bool Entity::GetMidHead( SD_Tools::Vector* Out )
{
	SD_Tools::Vector HeadBone;
	SD_Tools::Vector NeckBone;

	if (!this->GetBone(BONE_HEAD, &HeadBone))
		return false;
	if (!this->GetBone(BONE_NECK, &NeckBone))
		return false;

	*Out = SD_Tools::MathHelper::SubVector(HeadBone, NeckBone);
	Out->Multiply(1.6f);
	Out->Add(NeckBone);
	Out->z += 2.0f;
	return true;
}
